﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using XiamenQudianCoffee.model;

namespace XiamenQudianCoffee
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 制作
        /// </summary>
        public static bool isMake = true;//制作
        /// <summary>
        /// 预存
        /// </summary>
        public static bool isYC = true;//预存
        public static double MakingTypes = 0;
        public static string img_path = "";
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\IPorPortMessage.txt";
        public static string WarnPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Warn";
        public static string BreakPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"BreakDowm";
        public static string Printer = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Printer";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.url_start = porPortInfo.url_start;
                    IPorPortInfo.Instance.deviceID = porPortInfo.deviceID;
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.RobotIPRight = porPortInfo.RobotIPRight;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.PrinterCoffeeIP = porPortInfo.PrinterCoffeeIP;
                    IPorPortInfo.Instance.PrinterCofferPort = porPortInfo.PrinterCofferPort;
                    IPorPortInfo.Instance.EWMPortName = porPortInfo.EWMPortName;
                    IPorPortInfo.Instance.ZBPortName = porPortInfo.ZBPortName;
                    IPorPortInfo.Instance.ZBPortName2 = porPortInfo.ZBPortName2;
                    IPorPortInfo.Instance.CoffeePortName = porPortInfo.CoffeePortName;
                    IPorPortInfo.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    IPorPortInfo.Instance.SugarInit2 = porPortInfo.SugarInit2;
                    IPorPortInfo.Instance.SugarInit3 = porPortInfo.SugarInit3;
                    IPorPortInfo.Instance.SugarInit4 = porPortInfo.SugarInit4;
                    IPorPortInfo.Instance.SugarInit5 = porPortInfo.SugarInit5;
                    IPorPortInfo.Instance.SugarInit6 = porPortInfo.SugarInit6;
                    WuLiaoCoffeeSel.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    WuLiaoCoffeeSel.Instance.SugarInit2 = porPortInfo.SugarInit2;
                    WuLiaoCoffeeSel.Instance.SugarInit3 = porPortInfo.SugarInit3;
                    WuLiaoCoffeeSel.Instance.SugarInit4 = porPortInfo.SugarInit4;
                    WuLiaoCoffeeSel.Instance.SugarInit5 = porPortInfo.SugarInit5;
                    WuLiaoCoffeeSel.Instance.SugarInit6 = porPortInfo.SugarInit6;
                }
                if (!Directory.Exists(WarnPath))
                {
                    Directory.CreateDirectory(WarnPath);
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                WarnPath = WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
                if (!Directory.Exists(BreakPath))
                {
                    Directory.CreateDirectory(BreakPath);
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                BreakPath = BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
                Thread thr3 = new Thread(SocketClients.Instance.socketclient);
                thr3.IsBackground = true;
                thr3.Start();

                //Thread thread = new Thread(MainWindow.MessageShow);
                //thread.IsBackground = true;
                //thread.Start();
            }
            catch (Exception)
            {
                return;
            }
        }
        public static void MessageShow()
        {
            while (true)
            {
                try
                {
                    if (CoffeeMakes.Instance.coffeeMakes.Any(a => a.Type == 1 && a.PrestoredPosition != 0))
                    {
                        CoffeeMakes.Instance.QDMsg();
                    }
                    else
                    {
                        Devices device = new Devices();
                        string lists = JsonConvert.SerializeObject(device);
                        var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);
                        if (data != null)
                        {
                            Console.WriteLine(data);
                            Console.WriteLine(isMake);
                            if (isMake)
                            {
                                var msglist = JsonConvert.DeserializeObject<HTTPMsg>(data);
                                if (msglist != null)
                                {
                                    if (msglist.data.ToString().Length > 10)
                                    {
                                        var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                        if (list.Count > 0)
                                        {
                                            if (isMake)
                                            {
                                                if (list.Count > 0)
                                                {
                                                    var ddd = list.FirstOrDefault(c => c.typeList2.Any(a => (a.status == "3" || a.status == "4") && !CoffeeMakes.Instance.coffeeMakes.Any(b => b.OrderNum == c.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId) && !CoffeeMakes.Instance.coffeeMaking.Any(b => b.OrderNum == c.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId) && !CoffeeMakes.Instance.coffeeMaked.Any(b => b.OrderNum == c.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId)));
                                                    if (ddd == null) continue;
                                                    if (ddd.typeList2.Count > 0)
                                                    {

                                                        var make = ddd.typeList2.FirstOrDefault(a => (a.status == "3" || a.status == "4") && !CoffeeMakes.Instance.coffeeMakes.Any(b => b.OrderNum == ddd.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId) && !CoffeeMakes.Instance.coffeeMaking.Any(b => b.OrderNum == ddd.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId) && !CoffeeMakes.Instance.coffeeMaked.Any(b => b.OrderNum == ddd.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId));

                                                        if (make != null)
                                                        {
                                                            Console.WriteLine(make.num.ToString() + " " + DateTime.Now.ToString());
                                                            if (make.jsonCodeVal.Length > 4)
                                                            {
                                                                var coffeecode = JsonConvert.DeserializeObject<List<specification>>(make.jsonCodeVal);
                                                                if (coffeecode.Count > 0)
                                                                {
                                                                    var coffeeID = coffeecode.FirstOrDefault(a => a.classCode != "");
                                                                    if (coffeeID != null)
                                                                    {
                                                                        RobotOperate.CX_Start();
                                                                        Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                                        if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                                        {
                                                                            //if (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)
                                                                            CoffeeMaking(coffeeID.classCode, make, ddd.orderNum);
                                                                            Console.WriteLine("一单完成！");
                                                                            // CoffeeMaking("5003", null, "5003");
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                        }
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    //return;
                    Thread.Sleep(1000);
                }
            }
        }
        /// <summary>
        /// 取杯
        /// </summary>
        /// <param name="cupCode"></param>
        public static void QBMsg(int cupcode)
        {
            try
            {
                Thread.Sleep(200);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJB1_UP);
                Thread.Sleep(200);
                while (true)
                {
                    Console.WriteLine("daojiebeidian1 " + DateTime.Now.ToString());
                    IOMsgRobot.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDLB1);
                    Thread.Sleep(200);
                    if (IOMsgRobot.Instance.RobotBol)
                    {
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_KSZZ_Down);
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_KB_Down);
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJB1_Down);
                        break;
                    }
                }
                while (!IOMsg.Instance.CFBol)
                {
                    Thread.Sleep(100);
                }
                IOMsg.Instance.CFBol = false;
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.LB1_start);
                Thread.Sleep(500);
                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.LB1_end);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                IOMsg.Instance.CFBol = true;
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJCJB1_UP);
                Thread.Sleep(500);
                IOMsgRobot.Instance.RobotBol = false;
                while (true)
                {
                    Thread.Sleep(100);
                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDBJCWZ1);
                    Thread.Sleep(100);
                    if (IOMsgRobot.Instance.RobotBol)
                    {
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJCJB1_Down);
                        break;
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public static void CoffeeMakeNow(int coffeeCode)
        {
            try
            {
                switch (coffeeCode)
                {
                    case 1:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case 2:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.americano);
                        break;
                    case 3:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.AmericanoIce);
                        break;
                    case 4:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.Cappuccino);
                        break;
                    case 5:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.CappuccinoIce);
                        break;
                    case 6:
                    case 12:
                    case 14:
                    case 16:
                    case 18:
                    case 20:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.Latte);
                        break;
                    case 7:
                    case 13:
                    case 15:
                    case 17:
                    case 19:
                    case 21:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.LatteIce);
                        break;
                    case 8:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.AustralianWhite);
                        break;
                    case 9:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.AustralianWhiteIce);
                        break;
                    case 10:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.Mocha);
                        break;
                    case 11:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.MochaIce);
                        break;
                    case 22:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.Macchiato);
                        break;
                    case 23:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.MacchiatoIce);
                        break;
                    case 24:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.ChocolateMilk);
                        break;
                    case 25:
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.ChocolateMilkIce);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {
            }
        }
        public static void CoffeeMaking(string makecoffeeName, DeviceOrderQueue list, string orderNum, bool CoffeeBol = true)
        {
            try
            {
                if (!isMake) return;
                isMake = false;
                int sugarCode = 0;//糖型
                int cupCode = 0;//杯型

                int prepaidCode = 0;//预存
                int code = 0;
                var msgs = JsonConvert.DeserializeObject<List<specification>>(list.jsonCodeVal);
                Console.WriteLine(DateTime.Now.ToString() + "  cx10" + list.jsonCodeVal);
                if (msgs != null)
                {
                    if (msgs.Any(a1 => a1.SugarCode != ""))
                    {
                        sugarCode = msgs.Any(a1 => a1.SugarCode == "1") ? 1 : msgs.Any(a1 => a1.SugarCode == "2") ? 2 : msgs.Any(a1 => a1.SugarCode == "3") ? 3 : msgs.Any(a1 => a1.SugarCode == "4") ? 4 : msgs.Any(a1 => a1.SugarCode == "5") ? 5 : msgs.Any(a1 => a1.SugarCode == "6") ? 6 : 0;
                    }
                    if (msgs.Any(b => b.cupCode != ""))
                    {
                        cupCode = msgs.Any(b => b.cupCode == "1") ? 1 : msgs.Any(b => b.cupCode == "2") ? 2 : msgs.Any(b => b.cupCode == "3") ? 3 : 4;
                    }
                    if (msgs.Any(b => b.PrepaidCode != ""))
                    {
                        prepaidCode = msgs.Any(b => b.PrepaidCode == "1") ? 1 : 0;
                    }
                    if (msgs.Any(a => a.classCode != ""))
                    {
                        var cod = msgs.FirstOrDefault(a => a.classCode != "");
                        code = int.TryParse(cod.classCode, out int clas) ? clas : 0;
                    }
                }
                if (prepaidCode != 0)
                {
                    if (WuLiaoCoffeeSel.Instance.PrestoredPosition1_Sel_Bol && WuLiaoCoffeeSel.Instance.PrestoredPosition2_Sel_Bol && WuLiaoCoffeeSel.Instance.PrestoredPosition3_Sel_Bol && WuLiaoCoffeeSel.Instance.PrestoredPosition4_Sel_Bol && WuLiaoCoffeeSel.Instance.PrestoredPosition5_Sel_Bol && WuLiaoCoffeeSel.Instance.PrestoredPosition6_Sel_Bol && WuLiaoCoffeeSel.Instance.PrestoredPosition7_Sel_Bol && WuLiaoCoffeeSel.Instance.PrestoredPosition8_Sel_Bol && WuLiaoCoffeeSel.Instance.PrestoredPosition9_Sel_Bol)
                    {
                        CoffeeMakeModel coffeeMake = new CoffeeMakeModel();
                        coffeeMake.OrderGoodsId = list.id;
                        coffeeMake.OrderId = list.orderId;
                        coffeeMake.OrderNum = orderNum;
                        CoffeeMakes.Instance.coffeeMaked.Add(coffeeMake);
                        Console.WriteLine("***" + list.orderId + "&" + list.id + "&" + orderNum + "#1");
                        isMake = true;
                        return;
                    }
                    else
                    {
                        CoffeeMakes.Instance.coffeeMaked.Clear();
                        Console.WriteLine("***" + list.orderId + "&" + list.id + "&" + orderNum + "#1");
                    }
                }
                else
                {
                    if (!WuLiaoCoffeeSel.Instance.PushRodUP1_Sel_Bol && !WuLiaoCoffeeSel.Instance.PushRodUP2_Sel_Bol)
                    {
                        isMake = true;
                        return;
                    }
                }
                while (true)
                {
                    EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                    status.orderGoodsId = list.id;
                    status.orderId = list.orderId;
                    status.status = 4;
                    string lists = JsonConvert.SerializeObject(status);
                    var ddd1 = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                    if (ddd1 != null)
                    {
                        if (ddd1 != "")
                        {
                            var list10 = JsonConvert.DeserializeObject<ListMsg>(ddd1);
                            if (list10 != null)
                            {
                                if (list10.code == "0" || list10.code == "1")
                                {
                                    Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                    break;
                                }
                            }
                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                        }

                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(500);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 10;
                clientList.IsMake = false;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                int MidCode = code / 1000;
                int CoffeeCode = code % 1000;
                Thread.Sleep(200);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJB1_UP);
                if (MidCode == 52)
                {
                    Thread.Sleep(200);
                    if (WuLiaoCoffeeSel.Instance.Ice1_Bol)
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QZBJ1_UP);
                    else if (WuLiaoCoffeeSel.Instance.Ice2_Bol)
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QZBJ2_UP);
                }
                if (sugarCode == 1 || sugarCode == 2 || sugarCode == 3 || sugarCode == 4 || sugarCode == 5 || sugarCode == 6)
                {
                    Thread.Sleep(200);
                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJGTWZ1_UP);
                }
                Thread.Sleep(200);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_KSZZ_UP);
                Thread.Sleep(1000);
                QBMsg(cupCode);
                Thread.Sleep(1000);
                while (true)
                {
                    if (WuLiaoCoffeeSel.Instance.TakeCup1_Sel_Bol)
                    {
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_JBCG_UP);
                        Thread.Sleep(200);
                        if (sugarCode == 1 || sugarCode == 2 || sugarCode == 3 || sugarCode == 4 || sugarCode == 5 || sugarCode == 6)
                        {
                            IOMsgRobot.Instance.RobotBol = false;
                            while (true)
                            {
                                Thread.Sleep(200);
                                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDGTWZ1);
                                Thread.Sleep(200);
                                if (IOMsgRobot.Instance.RobotBol)
                                {
                                    break;
                                }
                            }
                            while (!IOMsg.Instance.CFBol) { Thread.Sleep(100); }
                            IOMsg.Instance.CFBol = false;
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            if (sugarCode == 1)
                            {
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT1_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit1 * 1000));
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT1_end);
                            }
                            else if (sugarCode == 2)
                            {
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT2_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit2 * 1000));
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT2_end);
                            }
                            else if (sugarCode == 3)
                            {
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT3_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit3 * 1000));
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT3_end);
                            }
                            else if (sugarCode == 4)
                            {
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT4_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit4 * 1000));
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT4_end);
                            }
                            else if (sugarCode == 5)
                            {
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT5_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit5 * 1000));
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT5_end);
                            }
                            else if (sugarCode == 6)
                            {
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT6_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit6 * 1000));
                                IOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT6_end);
                            }
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            IOMsg.Instance.CFBol = true;
                            Thread.Sleep(1000);
                            IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_GTWC_UP);
                            Thread.Sleep(200);
                            IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJGTWZ1_Down);
                        }
                        if (MidCode == 52)
                        {
                            try
                            {
                                IOMsgRobot.Instance.RobotBol = false;
                                while (true)
                                {
                                    Thread.Sleep(200);
                                    if (WuLiaoCoffeeSel.Instance.Ice1_Bol)
                                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDZBJ1);
                                    else if (WuLiaoCoffeeSel.Instance.Ice2_Bol)
                                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDZBJ2);
                                    Thread.Sleep(200);
                                    if (IOMsgRobot.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                Thread.Sleep(200);
                                if (WuLiaoCoffeeSel.Instance.Ice1_Bol)
                                {
                                    IceMakingMsg1.Instance.AllBol = false;
                                    Thread.Sleep(1000);
                                    IceMakingSerialPort1.Instance.SerialSend(IceMakingModel.Instance.IceMaking);
                                    Thread.Sleep(1000);
                                    IceMakingMsg1.Instance.AllBol = true;
                                    Thread.Sleep(4000);
                                    for (int i = 0; i < 2; i++)
                                    {
                                        if (!WuLiaoCoffeeSel.Instance.Weigh1_Sel_Bol)
                                        {
                                            IceMakingMsg1.Instance.AllBol = false;
                                            Thread.Sleep(1000);
                                            IceMakingSerialPort1.Instance.SerialSend(IceMakingModel.Instance.IceMaking3);
                                            Thread.Sleep(1000);
                                            IceMakingMsg1.Instance.AllBol = true;
                                            Thread.Sleep(2000);
                                        }
                                    }
                                }
                                else if (WuLiaoCoffeeSel.Instance.Ice2_Bol)
                                {
                                    IceMakingMsg2.Instance.AllBol = false;
                                    Thread.Sleep(1000);
                                    IceMakingSerialPort2.Instance.SerialSend(IceMakingModel.Instance.IceMaking);
                                    Thread.Sleep(1000);
                                    IceMakingMsg2.Instance.AllBol = true;
                                    Thread.Sleep(4000);
                                    for (int i = 0; i < 2; i++)
                                    {
                                        if (!WuLiaoCoffeeSel.Instance.Weigh2_Sel_Bol)
                                        {
                                            IceMakingMsg2.Instance.AllBol = false;
                                            Thread.Sleep(1000);
                                            IceMakingSerialPort2.Instance.SerialSend(IceMakingModel.Instance.IceMaking3);
                                            Thread.Sleep(1000);
                                            IceMakingMsg2.Instance.AllBol = true;
                                            Thread.Sleep(2000);
                                        }
                                    }
                                }
                                Thread.Sleep(200);
                                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_JBWC_UP);
                                Thread.Sleep(200);
                                if (WuLiaoCoffeeSel.Instance.Ice1_Bol)
                                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QZBJ1_Down);
                                else if (WuLiaoCoffeeSel.Instance.Ice2_Bol)
                                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QZBJ2_Down);
                            }
                            catch (Exception)
                            {
                            }
                        }
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_JBCG_Down);
                        Thread.Sleep(200);
                        Console.WriteLine(DateTime.Now.ToString() + "  cx17");
                        Console.WriteLine(DateTime.Now.ToString() + "  cx18");
                        break;
                    }
                    else
                    {
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_KB_UP);
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJCJB1_Down);
                        Thread.Sleep(200);
                        QBMsg(cupCode);
                    }
                }
                while (!coffeeMsg.Instance.isMaking) { Thread.Sleep(10); }
                Thread.Sleep(200);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJKF_UP);
                Thread.Sleep(200);
                IOMsgRobot.Instance.RobotBol = false;
                while (true)
                {
                    Thread.Sleep(200);
                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDKFJ);
                    Thread.Sleep(200);
                    if (IOMsgRobot.Instance.RobotBol)
                    {
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_GTWC_Down);
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_JBWC_Down);
                        Thread.Sleep(200);
                        break;
                    }
                }
                coffeeMsg.Instance.AllBol = false;
                isYC = true;
                Thread.Sleep(200);
                //CoffeeMakeNow(2);
                CoffeeMakeNow(CoffeeCode);
                Thread.Sleep(500);
                coffeeMsg.Instance.AllBol = true;
                //while (isYC)
                //{
                //    Thread.Sleep(100);
                //}
                Thread.Sleep(1000);
                while (coffeeMsg.Instance.isMaking && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                Thread.Sleep(1000);
                while ((!coffeeMsg.Instance.isMaking && !coffeeMsg.Instance.IsJBol) && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJKF_Down);
                //while (!isYC) { Thread.Sleep(10); }
                //isYC = false;
                // isMake = false;
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_KFWC_UP);
                Thread.Sleep(200);
                if (WuLiaoCoffeeSel.Instance.Lid1_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionBehind1_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionDown1_Sel_Bol)
                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QG1_UP);
                else if (WuLiaoCoffeeSel.Instance.Lid2_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionBehind2_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionDown2_Sel_Bol)
                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QG2_UP);
                else if (WuLiaoCoffeeSel.Instance.Lid3_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionBehind3_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionDown3_Sel_Bol)
                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QG3_UP);
                IOMsgRobot.Instance.RobotBol = false;
                int LGInit = 0;
                while (true)
                {
                    Thread.Sleep(100);
                    if (WuLiaoCoffeeSel.Instance.Lid1_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionBehind1_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionDown1_Sel_Bol)
                    {
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDG1);
                        LGInit = 1;
                    }
                    else if (WuLiaoCoffeeSel.Instance.Lid2_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionBehind2_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionDown2_Sel_Bol)
                    {
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDG2);
                        LGInit = 2;
                    }
                    else if (WuLiaoCoffeeSel.Instance.Lid3_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionBehind3_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionDown3_Sel_Bol)
                    {
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDG3);
                        LGInit = 3;
                    }
                    Thread.Sleep(200);
                    if (IOMsgRobot.Instance.RobotBol)
                    {
                        if (WuLiaoCoffeeSel.Instance.Lid1_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionBehind1_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionDown1_Sel_Bol)
                            IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QG1_Down);
                        else if (WuLiaoCoffeeSel.Instance.Lid2_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionBehind2_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionDown2_Sel_Bol)
                            IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QG2_Down);
                        else if (WuLiaoCoffeeSel.Instance.Lid3_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionBehind3_Sel_Bol && WuLiaoCoffeeSel.Instance.LidOffPositionDown3_Sel_Bol)
                            IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QG3_Down);
                        Thread.Sleep(200);
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_KFWC_Down);
                        Thread.Sleep(200);
                        break;
                    }
                }
                //while (!IOMsg.Instance.CFBol) { Thread.Sleep(100); }
                //IOMsg.Instance.CFBol = false;
                //IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                CoffeeMakes.Instance.YGMsg(LGInit);
                Thread.Sleep(200);
                int CCInit = 0;
                if (prepaidCode == 0 || (prepaidCode == 1 && CoffeeMakes.Instance.coffeeMakes.Any(a => a.Type == 1 && a.OrderId == list.orderId && a.OrderGoodsId == list.id && a.OrderNum == orderNum)))
                {
                    if (WuLiaoCoffeeSel.Instance.PushRodUP1_Sel_Bol)
                    {
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCC1_UP);
                        CCInit = 1;
                    }
                    else if (WuLiaoCoffeeSel.Instance.PushRodUP2_Sel_Bol)
                    {
                        IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCC2_UP);
                        CCInit = 2;
                    }
                    IOMsgRobot.Instance.RobotBol = false;
                    while (true)
                    {
                        Thread.Sleep(200);
                        if (CCInit == 1)
                            IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDCCK1);
                        else if (CCInit == 2)
                            IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_DDCCK2);
                        Thread.Sleep(200);
                        if (IOMsgRobot.Instance.RobotBol)
                        {
                            if (CCInit == 1)
                            {
                                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCC1_Down);
                            }
                            else if (CCInit == 2)
                            {
                                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCC2_Down);
                            }
                            Thread.Sleep(200);
                            IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_YGWC_Down);
                            Thread.Sleep(200);
                            break;
                        }
                    }
                    TestDelegateInvoke testDelegate = new TestDelegateInvoke(CoffeeMakes.Instance.MakeMealMsg);
                    testDelegate.BeginInvoke(orderNum, CCInit, list.id, list.orderId, null, null);
                    Console.WriteLine("出餐结束");
                    CoffeeMakeModel makeModel = new CoffeeMakeModel();
                    makeModel.OrderGoodsId = list.id;
                    makeModel.OrderId = list.orderId;
                    makeModel.OrderNum = orderNum;
                    CoffeeMakes.Instance.coffeeMaking.Add(makeModel);
                    Console.WriteLine("这单完事");
                }
                else
                {
                    Thread.Sleep(200);
                    IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_YGWC_Down);
                    Thread.Sleep(200);
                    CoffeeMakes.Instance.CDMsg(list, orderNum);
                }
                //ClientList clientList1 = new ClientList();
                //clientList1.MsgBol = WuLiaoCoffeeSel.Instance;
                //clientList1.code = 10;
                //clientList1.IsMake = false;
                //SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList1)));
                GC.Collect();
                RobotOperate.CX_Start();
                Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                {
                    if (WuLiaoCoffeeSel.Instance.RobotWarn != true)
                    {
                        WuLiaoCoffeeSel.Instance.RobotWarn = true;
                    }
                }
                else
                {
                    if (WuLiaoCoffeeSel.Instance.RobotWarn != false)
                    {
                        WuLiaoCoffeeSel.Instance.RobotWarn = false;
                    }
                }
                ClientList clientList2 = new ClientList();
                clientList2.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList2.code = 2;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
                Console.WriteLine("这单完事最终");
                isMake = true;
            }
            catch (Exception)
            {
                return;
            }
        }
        public delegate void TestDelegateInvoke(string orderNum, int ccInit, int ordergoodsid, int orderid);
        public static string EmailError()
        {
            try
            {
                string msg = "<style>table{ border: 1px solid black; border-collapse: collapse; }table td{border: 1px solid #000000; padding: 5px 15px;font-size:14px;}</style><table>";
                if (WuLiaoCoffeeSel.Instance.CoffeeBean1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean1</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (!WuLiaoCoffeeSel.Instance.CoffeeBean2_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean2</td><td>adequate materials</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Milk1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Milk1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Milk1</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                //if (WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol)
                //    msg += "<tr><td style='font-weight:bold;'>Cup2</td><td>adequate materials</td></tr>";
                //else
                //    msg += "<tr><td style='font-weight:bold;'>Cup2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Milk2_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Milk2</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Milk2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar1</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose2_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar2</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose3_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar3</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar3</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose4_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar4</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar4</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose5_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar5</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar5</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose6_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar6</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar6</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                //if (!IsMsgError)
                //    msg += "<tr><td style='font-weight:bold;'>Coffee Maker</td><td>Ready</td></tr>";
                //else
                //    msg += "<tr><td style='font-weight:bold;'>Coffee Maker</td><td style='color:#FF0000;'>NotReady</td></tr>";
                msg += "</table>";
                return msg;
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
